package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.common.Login.TingqLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackStatVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album/trackInfo")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;

	/**
	 * 上传声音
	 * @param file
	 * @return
	 */
	@Operation(summary = "音频上传到tencent云VOD")
	@PostMapping("/uploadTrack")
	public Result<Map<String,String>> uploadFile(MultipartFile file){

	Map<String,String> map  =	trackInfoService.uploadFile(file);
	return  Result.ok(map);
	}

	/**
	 * 增 -> 保存声音
	 * @param trackInfoVo 封装数据
	 * @return
	 */
	//saveTrackInfo
	@TingqLogin
	@Operation(summary = "保存音频")
	@PostMapping("/saveTrackInfo")
	public Result saveTrackInfo(@RequestBody @Validated TrackInfoVo trackInfoVo){

		//当前用户信息
		Long userId = AuthContextHolder.getUserId();


		trackInfoService.saveTrackInfo(userId,trackInfoVo);
		return Result.ok();


	}

	/**
	 * 分页查询 通过查询用户id 查询当前用户下的声音信息 + 声音的统计信息
	 * @param page 页大小
	 * @param limit 每页记录数
	 * @param trackInfoQuery 查询条件
	 * @return
	 */
	@TingqLogin
	@Operation(summary = "获取当前登录声音分页列表")
	@PostMapping("/findUserTrackPage/{page}/{limit}")
	public Result<Page<TrackListVo>> getUserTrackByPage(@PathVariable int page, @PathVariable int limit, @RequestBody TrackInfoQuery trackInfoQuery) {
		//1.获取用户ID
//        Long userId = AuthContextHolder.getUserId();
//        //2.封装分页查询对象
//        trackInfoQuery.setUserId(userId);  在业务方法中执行
		//3.调用业务层进行分页
		Page<TrackListVo> PageInfo = new Page<>(page, limit);
		PageInfo = trackInfoService.getUserTrackByPage(PageInfo, trackInfoQuery);
		return Result.ok(PageInfo);
	}


	/**
	 * 根据声音ID查询声音信息
	 * @param id 声音ID
	 * @return 声音信息
	 */
	@Operation(summary = "根据声音ID查询声音信息")
	@GetMapping("/getTrackInfo/{id}")
	public Result<TrackInfo> getTrackInfo(@PathVariable Long id) {
		TrackInfo trackInfo = trackInfoService.getById(id);
		return Result.ok(trackInfo);
	}

	@TingqLogin
	@Operation(summary = "修改声音信息")
	@PutMapping("/updateTrackInfo/{id}")
	public Result updateTrackInfo(@PathVariable Long id, @RequestBody @Validated TrackInfoVo trackInfoVo){
		trackInfoService.updateTrackInfo(id, trackInfoVo);
		return Result.ok();
	}

	/**
	 * 根据声音ID删除声音
	 * @param id
	 * @return
	 */
	@TingqLogin
	@Operation(summary = "根据声音ID删除声音")
	@DeleteMapping("/removeTrackInfo/{id}")
	public Result removeTrackInfo(@PathVariable Long id){
		trackInfoService.removeTrackInfo(id);
		return Result.ok();
	}

	/**
	 * 查询当前用户查看专辑声音分页列表
	 * @param albumId 专辑id
	 * @param page 页码
	 * @param limit 页大小
	 * @return 声音列表
	 */
	@TingqLogin(required = false)
	@Operation(summary = "查询当前用户查看专辑声音分页列表")
	@GetMapping("/findAlbumTrackPage/{albumId}/{page}/{limit}")
	public Result<Page<AlbumTrackListVo>> getUserAlbumTrackPage(@PathVariable Long albumId, @PathVariable int page, @PathVariable int limit) {
		Long userId = AuthContextHolder.getUserId();

		Page<AlbumTrackListVo> pageInfo = new Page<>(page, limit);

		pageInfo  = 	trackInfoService.getUserAlbumTrackPage(pageInfo,userId,albumId);

		return Result.ok(pageInfo);
	}

	/**
	 * 根据声音id,统计当前声音的点赞 评论 右下角的爱心 评论数量 声音的统计信息
	 * @param trackId
	 * @return
	 */
	@Operation(summary = "根据声音id,统计当前声音的点赞 评论 ...")
	@GetMapping("/getTrackStatVo/{trackId}")
	public Result<TrackStatVo> getTrackStatVo(@PathVariable Long trackId){
		return Result.ok(trackInfoService.getTrackStatVo(trackId));
	}

	/**
	 * 查询当前用户分集购买声音列表
	 * @param trackId
	 * @return
	 */
	@TingqLogin
	@GetMapping("/findUserTrackPaidList/{trackId}")
	@Operation(summary = "查询当前用户分集购买声音列表")
	public Result<List<Map<String, Object>>> getUserTrackWaitPayList(@PathVariable Long trackId) {
		Long userId = AuthContextHolder.getUserId();
		List<Map<String, Object>> list = trackInfoService.getUserTrackWaitPayList(userId, trackId);
		return Result.ok(list);
	}


	/**
	 * 代购声音列表 声音id + 购买的数量 -渲染声音结算页面
	 * @param trackId 声音id
	 * @param trackCount 声音购买数量
	 * @return
	 */
	@TingqLogin
	@GetMapping("/findPaidTrackInfoList/{trackId}/{trackCount}")
	@Operation(summary = "待购声音列表-用于渲染声音结算页面" )
	public Result<List<TrackInfo>> getWaitPayTrackInfoList(@PathVariable Long trackId, @PathVariable Integer trackCount) {
		Long userId = AuthContextHolder.getUserId();
		List<TrackInfo> list = trackInfoService.getWaitPayTrackInfoList(userId,trackId,trackCount);
		return Result.ok(list);
	}
}

